package baiduTTS.service;

import baiduTTS.exception.BaseException;
import baiduTTS.utils.ConnUtil;
import baiduTTS.utils.TokenHolder;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author songmingsong
 * @date 2023/6/29
 */
public class TTS {
    private String appKey;
    private String secretKey;
    private int per;
    private int spd;
    private int pit;
    private int vol;
    // 调用地址
    public String url = "https://tsn.baidu.com/text2audio";
    // 用户唯一标识，用来区分用户，长度为60以内
    private String cuid = "0322java";

    public TTS(String appKey, String secretKey, int per, int spd, int pit, int vol) {
        this.appKey = appKey;
        this.secretKey = secretKey;
        this.per = per;
        this.spd = spd;
        this.pit = pit;
        this.vol = vol;
    }

    /**
     * 获取MP3格式文件
     *
     * @param text 需要生成的文本
     * @return 文件地址
     * @throws Exception
     */
    public String getMp3(String text) throws Exception {
        HttpURLConnection conn = getConn(text);
        conn.setConnectTimeout(5000);
        String contentType = conn.getContentType();
        if (contentType.contains("mp3")) {
            byte[] bytes = ConnUtil.getResponseBytes(conn);
            // 存在项目根目录下
            File file = new File("result.mp3");
            FileOutputStream os = new FileOutputStream(file);
            os.write(bytes);
            os.close();
            return file.getAbsolutePath();
        } else {
            String res = ConnUtil.getResponseString(conn);
            throw new BaseException(res);
        }
    }

    /**
     * 获取MP3格式文件
     *
     * @param text 需要生成的文本
     * @param path 需要生成的mp3地址,如：result.mp3
     * @return 文件地址
     * @throws Exception
     */
    public String getMp3(String text, String path) throws Exception {
        HttpURLConnection conn = getConn(text);
        conn.setConnectTimeout(5000);
        String contentType = conn.getContentType();
        if (contentType.contains("mp3")) {
            byte[] bytes = ConnUtil.getResponseBytes(conn);
            // 存在项目根目录下
            File file = new File(path);
            FileOutputStream os = new FileOutputStream(file);
            os.write(bytes);
            os.close();
            return file.getAbsolutePath();
        } else {
            String res = ConnUtil.getResponseString(conn);
            throw new BaseException(res);
        }
    }

    /**
     * 获取文件流
     *
     * @param text
     * @return
     * @throws Exception
     */
    public InputStream getMp3Stream(String text) throws Exception {
        HttpURLConnection conn = getConn(text);
        conn.setConnectTimeout(5000);
        String contentType = conn.getContentType();
        if (contentType.contains("mp3")) {
            return ConnUtil.getResponseStream(conn);
        } else {
            String res = ConnUtil.getResponseString(conn);
            throw new BaseException(res);
        }
    }

    private HttpURLConnection getConn(String text) throws Exception {
        TokenHolder holder = new TokenHolder(appKey, secretKey, TokenHolder.ASR_SCOPE);
        holder.resfresh();
        String token = holder.getToken();

        String url2 = url + "?tex=" + ConnUtil.urlEncode(text);
        url2 += "&per=" + per;
        url2 += "&spd=" + spd;
        url2 += "&pit=" + pit;
        url2 += "&vol=" + vol;
        url2 += "&cuid=" + cuid;
        url2 += "&tok=" + token;
        url2 += "&lan=zh&ctp=1";
        return (HttpURLConnection) new URL(url2).openConnection();
    }
}
